__author__ = 'Administrator'

from ASIN import *
import simplejson as json
import urllib
import math

def feature1():
    page_count = 20
    product = get_ASIN()
    count = getCategoryCount()
    page = int(math.ceil(count/page_count))
    file_object = file('../txt_package/women_jeans_feature.txt', 'w')
    for i in range(0,page-1):
        product_page = product.write_info(category_name(), i)
        for j in range(0,19):
            feature2 = featureKey(product_page[j])
            list = [product_page[j],feature2]
            print list
            file_object.write(product_page[j]+'\n')
            for k in range(0,len(feature2)-1):
                file_object.write('    '+feature2[k])
            file_object.write('\n')
    file_object.close( )
    
def get_product_data(asin):
    target_url = 'http://112.124.1.3:8004/api/commodity/'+asin
    return json.loads(urllib.urlopen(target_url).read())

def featureKey(asin):
    product_data = get_product_data(asin)
    return product_data['productInfo'][0]['feature']
    
def getCategoryCount():
    target_url = 'http://112.124.1.3:8004/api/commodity/count/?category_name='+category_name()
    return json.loads(urllib.urlopen(target_url).read())['count']

def category_name():
    return 'Clothing $ Accessories>Women>Jeans'

if __name__ == '__main__':
    feature1()